﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;

namespace Linkmeme
{
    public class ProviderValidator
    {
        public static bool CheckUserFromProvider(int providerId, string userName)
        {
            if (providerId != 1) return true;   //temp

            bool userExist = false;
            WebClient wc = new WebClient();
            try 
	        {	        
        		 XElement result = XElement.Parse(
                        System.Text.Encoding.UTF8.GetString(
                        wc.DownloadData(GetMethodUrl(providerId, userName)
                        )));
                 userExist = (result.Attribute("stat").Value == "ok");

                 
                 
	        }
	        catch (Exception ex)
	        {
                userExist = false;
	        }

            return userExist;
        }

        #region Privates

        private static string GetMethodUrl(int providerId, string userName)
        {
            string url = string.Empty;
            switch (providerId)
            {
                case 1: //yupoo
                    url = string.Format("http://www.yupoo.com/api/rest/?method=yupoo.people.findByUsername&api_key={0}&username={1}", ConfigurationManager.AppSettings["YupooApiKey"], userName);  //"db08482ffa13e5bf1d994514fe8ad66b"
                    break;
                default:
                    break;
            }
            return url;
        }
        #endregion
    }
}
